package com.daichi.yygh.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.daichi.yygh.enums.RefundStatusEnum;
import com.daichi.yygh.model.order.PaymentInfo;
import com.daichi.yygh.model.order.RefundInfo;
import com.daichi.yygh.order.mapper.RefundInfoMapper;
import com.daichi.yygh.order.service.RefundInfoService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {
    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
        RefundInfo refundInfo = this.getOne(new QueryWrapper<RefundInfo>()
                .eq("order_id", paymentInfo.getOrderId())
                .eq("payment_type", paymentInfo.getPaymentType()));
        if (refundInfo != null) {
            return refundInfo;
        }

        RefundInfo saveRefund = new RefundInfo();
        saveRefund.setCreateTime(new Date());
        saveRefund.setOrderId(paymentInfo.getOrderId());
        saveRefund.setPaymentType(paymentInfo.getPaymentType());
        saveRefund.setOutTradeNo(paymentInfo.getOutTradeNo());
        saveRefund.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());
        saveRefund.setSubject(paymentInfo.getSubject());
        saveRefund.setTotalAmount(paymentInfo.getTotalAmount());

        this.save(saveRefund);

        return saveRefund;
    }
}
